import json

class ConfigureParser:

    def __init__(self, filepath: str = ""):
        self._filepath = filepath
        self._conf = {}

    def loadConfig(self, filepath: str):
        self._filepath = filepath
        try:
            f = open(filepath, "r")
        except:
            DebugPrint(f"file \"{self._filepath}\" not found.")
            return False
        jsondata = f.read()
        try:
            data = json.loads(jsondata)
        except json.JSONDecodeError as e:
            DebugPrint(e)
            return False
        self._conf = data
        return True
    
    def getData(self, data: dict, k, defaultval):
        return data.get(k, defaultval)

    def get(self, k, defaultval = None):
        if "." not in k:
            return self._conf.get(k, defaultval)
        klist = k.split(".")
        data = self._conf
        for i in range(len(klist)):
            k = klist[i]
            if i == len(klist) - 1:
                return self.getData(data, k, defaultval)
            else:
                data = self.getData(data, k, {})
